package com.web.xbq;

import com.entity.CatPageManager;
import com.entity.CatPost;
import com.entity.PageManager;
import com.entity.User;
import com.service.xbq.HeadService;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/head")
@MultipartConfig
public class HeadServelet extends HttpServlet {
    //request对象
    private HttpServletRequest request;
    //response对象
    private HttpServletResponse response;
    HeadService headService = new HeadService();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        this.request = request;
        this.response = response;
        String method = request.getParameter("method");
        String username = request.getParameter("username");
       // System.out.println(username);
        System.out.println(method);
        if (method ==null){
            method ="";
        }
        switch (method) {
            case "updataMessage": updataMessage(); break;
            case "myInformation": myInformation(); break;
            case "logOut": logOut(); break;
            case "personal":personal();break;
            case "querypassword":querypassword();break;
            case "passowrd":passowrd();break;
            case "tomypostPage":tomypostPage();break;
            case "deletePost":deletePost();break;
            case "toinformation2":toinformation2();break;
            case "toshowPage":toshowPage();break;
            case "examination":examination();break;
        }
    }

    private void examination() throws ServletException, IOException {
        String username = request.getParameter("username");
        CatPageManager catpm2 = WebUtils.getBean(CatPageManager.class, request);
        int totalCount = headService.queryUserTotalCount2(username);
        catpm2.setTotalCount(totalCount);//总数据数
        catpm2.setPages();//设置总页数
        HttpSession session = request.getSession();
        List<CatPost> catPosts = headService.queryCatPost2(catpm2, username);
        session.setAttribute("postpm2", catpm2);
        session.setAttribute("mycatpost2",catPosts);
        request.getRequestDispatcher("jsp/hs/examination.jsp").forward(request,response);
    }

    private void toshowPage() throws ServletException, IOException {
        String username = request.getParameter("username");
        CatPageManager catpm = WebUtils.getBean(CatPageManager.class, request);
        int totalCount = headService.queryUserTotalCount(username);
        catpm.setTotalCount(totalCount);//总数据数
        catpm.setPages();//设置总页数
        HttpSession session = request.getSession();
        List<CatPost> catPosts = headService.queryCatPost(catpm, username);
        session.setAttribute("postpm", catpm);
        session.setAttribute("mycatpost",catPosts);
        request.getRequestDispatcher("jsp/hs/myPost.jsp").forward(request,response);
    }

    private void toinformation2() throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("postid"));
        CatPost catPost2 = headService.queryCatpost(id);
        request.getSession().setAttribute("catPost2",catPost2);
        request.getRequestDispatcher("jsp/hs/releaseInformation2.jsp").forward(request,response);
    }

    private void deletePost() throws IOException, ServletException {
        int id = Integer.parseInt(request.getParameter("id"));
        String pn = request.getParameter("pn");
        CatPageManager pm = (CatPageManager) request.getSession().getAttribute("postpm");
        if (pm != null) {
            pm.setTotalCount(pm.getTotalCount()-1);
            pm.setPages();
            //删除之后，正好最后一页没了，直接使用最后一页当作当前页
            pm.setPn(pm.getPn()>=pm.getPages()?pm.getPages():pm.getPn());
        }
        int result = headService.delete(id);
        request.getSession().setAttribute("msg", result>0?"删除成功":"删除失败");
        request.getSession().setAttribute("booleans", result>0?"true":"false");
        request.getSession().setAttribute("postpm",pm);
        request.getRequestDispatcher("jsp/hs/myPost.jsp").forward(request,response);
//        response.sendRedirect("head?method=tomypostPage&username="+username+"&pn="+((pm==null)?1:pm.getPn()));
    }




    private void tomypostPage() throws ServletException, IOException {
        String username = request.getParameter("username");
        CatPageManager catpm = WebUtils.getBean(CatPageManager.class, request);
        int totalCount = headService.queryUserTotalCount(username);
        catpm.setTotalCount(totalCount);//总数据数
        catpm.setPages();//设置总页数
        HttpSession session = request.getSession();
        List<CatPost> catPosts = headService.queryCatPost(catpm, username);
        session.setAttribute("postpm", catpm);
        session.setAttribute("mycatpost",catPosts);
        request.getRequestDispatcher("jsp/hs/myPost.jsp").forward(request,response);
    }

    private void passowrd() throws ServletException, IOException {
    String newPassword =  request.getParameter("confirm_new_password");
        int id = Integer.parseInt(request.getParameter("id"));
        int i = headService.updatePassword(id, newPassword);
        HttpSession session = request.getSession();
        // session.removeAttribute("user");
        session.setAttribute("booleans","true");
        session.setAttribute("msg",i>0?"修改成功":"修改失败" );
        request.getRequestDispatcher("jsp/hs/password.jsp").forward(request,response);
    }

    private void querypassword() {
      String password = request.getParameter("password");
      int id = Integer.parseInt(request.getParameter("id"));
        User user = headService.queryUser(id);
        int i = headService.password(password, user);
        WebUtils.sendMSG(response, (i >0)?"1":"-1");
    }

    private void personal() {

    }

    private void logOut() throws IOException, ServletException {
        HttpSession session = request.getSession();
        //session.invalidate();//清空，销毁session
        session.removeAttribute("user");
        session.removeAttribute("postNum");
        session.setAttribute("booleans","true");
        session.setAttribute("title","注销提醒");
        session.setAttribute("msg","注销成功，请重新登录");

    //    response.sendRedirect("catlogin?method=toLoginPage");
        request.getRequestDispatcher("catlogin?method=toLoginPage").forward(request,response);
    }

    private void myInformation() {
    }



    private void updataMessage() {
      //  User user = WebUtils.getBean(User.class, request);
        try {
            String name = request.getParameter("name");
            String telephone = request.getParameter("telephone");
            String qq = request.getParameter("qq");
            String wx = request.getParameter("wx");
            String Email = request.getParameter("Email");
//            String pic2 = request.getParameter("pic");
//            System.out.println("pic2:"+pic2);
            String pic = WebUtils.uploadFile(request, "pic", WebUtils.SAVE_PATH);
            System.out.println("pic:"+pic);
            String id = request.getParameter("id");
            int id2 = id==null?0:Integer.parseInt(id);

           int result =headService.updateMessage(id2,name,telephone,qq,wx,Email,pic);
            System.out.println(result);
            //User bean = WebUtils.getBean(User.class, request);
            //int update = headService.update(bean);
          //  System.out.println("update"+update);
            User user2 =headService.queryUser(id2);
            HttpSession session = request.getSession();
           // session.removeAttribute("user");
            session.setAttribute("user",user2);
            session.setAttribute("booleans","true");
            session.setAttribute("updataMesage",result>0?"保存成功":"保存失败" );

            request.getRequestDispatcher("jsp/hs/modify.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
